package com.ruoyi.worklog.controller;


import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.worklog.domain.WlProject;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.worklog.domain.WlStaffProject;
import com.ruoyi.worklog.service.StaffProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * @Author cw
 * @Date 2022/7/21 - 07 - 21
 * @Description com.ruoyi.worklog.controller
 */

@RestController
@RequestMapping("/worklog/staffAndProject")
public class staffProjectController extends BaseController {
    @Autowired
    private StaffProjectService staffProjectService;

    /**
     * 查询员工-项目关系列表
     */


//    @PreAuthorize("@ss.hasPermi('worklog:project:Projectlist')")
    @GetMapping("/projectids/{staffId}")
    public TableDataInfo Projectlist(@PathVariable("staffId") int staffId){

        List<Integer> projectIdList = staffProjectService.selectProjectIds(staffId);
        return getDataTable(projectIdList);
    }

   // @PreAuthorize("@ss.hasPermi('worklog:project:Stafflist')")
    @GetMapping("/staffids/{projectId}")
    public TableDataInfo Stafflist(@PathVariable("projectId") int projectId){
        List<Long> staffIdList = staffProjectService.selectStaffIds(projectId);
        return getDataTable(staffIdList);
    }




    /**
     * 获取员工-项目关系详细信息
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:query')")
    @PostMapping("/getinfo")
    public AjaxResult getInfo(@Validated @RequestBody WlStaffProject wlStaffProject)
    {
        return AjaxResult.success(staffProjectService.selectWlStaffProject(wlStaffProject));
    }



    /**
     * 新增员工-项目关系
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:add')")
//    @Log(title = "员工-项目关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody WlStaffProject wlStaffProject)
    {
        if(wlStaffProject.getRole()==null|| wlStaffProject.getRole().equals("")){
            return AjaxResult.error("role 不能为空");
        }
        return toAjax(staffProjectService.insertWlStaffProject(wlStaffProject));
    }

    /**
     * 修改员工-项目关系
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:edit')")
//    @Log(title = "员工-项目关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody WlStaffProject wlStaffProject)
    {

        return toAjax(staffProjectService.updateWlStaffProject(wlStaffProject));
    }

//    @PreAuthorize("@ss.hasPermi('worklog:project:remove2')")
//    @Log(title = "员工-项目关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@Validated @RequestBody WlStaffProject wlStaffProject)
    {
        return toAjax(staffProjectService.deleteWlStaffProject(wlStaffProject));
    }

    /**
     * 删除员工-项目关系
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:remove')")
   // @Log(title = "员工-项目关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/staff/{staffId}")
    public AjaxResult removebystaff(@PathVariable long staffId)
    {
        return toAjax(staffProjectService.deleteWlStaffProjectByStaffId(staffId));
    }

    @DeleteMapping("/project/{projectId}")
    public AjaxResult removebyproject(@PathVariable int projectId)
    {
        return toAjax(staffProjectService.deleteWlStaffProjectByProjectId(projectId));
    }


}
